package com.muy.redis.view.window.redis.tree;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.project.Project;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.concurrency.NonUrgentExecutor;
import com.muy.redis.common.tree.MTTreeCell;
import com.muy.redis.common.tree.TreeCellEventExeContext;
import com.muy.redis.common.tree.TreeCellEventHandle;
import com.muy.redis.common.tree.TreePanelMark;
import com.muy.redis.common.tree.actions.MrRedisReloadKeyValueAction;
import com.muy.redis.utils.GuiUtils;
import com.muy.redis.utils.JTreeUtils;
import com.muy.redis.utils.MrRedisConnectionUtils;
import com.muy.redis.utils.RedisPoolManager;
import com.muy.redis.view.component.MrRedisEditorJpanelLeft;
//import com.muy.redis.view.window.redis.bean.KeyInfo;
import com.muy.redis.view.window.redis.bean.MrRedisConnectionParam;
import com.muy.redis.view.window.redis.bean.MrRedisDb;
import com.muy.redis.view.window.redis.enums.RedisValueTypeEnum;
import com.muy.redis.view.window.redis.view.MrRedisValueDisplayPanel;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.util.concurrent.CompletableFuture;

/**
 * @Author jiyanghuang
 * @Date 2022/8/5 00:07
 */
public class MTTreeCellMrRedisKey implements MTTreeCell {

    private Project project;

    private String redisKey;

//    private KeyInfo keyInfo;

    private MrRedisValueDisplayPanel valueDisplayPanel;

    private MrRedisConnectionParam mrRedisConnectionParam;

    private MrRedisDb mrRedisDb;

    private RedisValueTypeEnum redisValueTypeEnum;

    public MTTreeCellMrRedisKey(Project project, MrRedisDb mrRedisDb, MrRedisConnectionParam mrRedisConnectionParam, String redisKey, RedisValueTypeEnum redisValueTypeEnum) {
        this.project = project;
        this.redisKey = redisKey;
        this.mrRedisDb = mrRedisDb;
        this.mrRedisConnectionParam = mrRedisConnectionParam;
        this.redisValueTypeEnum = redisValueTypeEnum;
    }

    @Override
    public String cellShow() {
        return redisKey;
    }

    @Override
    public Icon iconSelected() {
        if (null != redisValueTypeEnum) {
            return redisValueTypeEnum.getIcon();
        }
        return AllIcons.Hierarchy.Subtypes;
    }

    @Override
    public Icon iconUnselected() {
        if (null != redisValueTypeEnum) {
            return redisValueTypeEnum.getIcon();
        }
        return AllIcons.Hierarchy.Subtypes;
    }

    @Override
    public SimpleTextAttributes fontAttributes() {
        if (null != redisValueTypeEnum) {
            return redisValueTypeEnum.getSimpleTextAttributes();
        }
        return null;
    }

    @Override
    public void doubleClick(JTree tree, DefaultMutableTreeNode mutableTreeNode, TreePanelMark treePanelMark) {
        // 根据key的不同类型, 组装不同的valueDisplayPanel
        MrRedisEditorJpanelLeft mrRedisEditorJpanelLeft = (MrRedisEditorJpanelLeft) treePanelMark;
        String key = redisKey;

        /**
         * value 展示区
         */
        if (null == valueDisplayPanel) {
            valueDisplayPanel = MrRedisValueDisplayPanel.getInstance();
            valueDisplayPanel.setMtTreeCellMrRedisKey(this);
            valueDisplayPanel.setMinimumSize(new Dimension(100, 100));
        }
        JBScrollPane valueDisplayScrollPanel = new JBScrollPane(valueDisplayPanel);
        valueDisplayScrollPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        valueDisplayScrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

//        LoadingDecorator loadingDecorator = new LoadingDecorator(valueDisplayScrollPanel, project, 0);
//        splitterContainer.setSecondComponent(loadingDecorator.getComponent());
        RedisPoolManager redisPoolManager = MrRedisConnectionUtils.findRedisPoolManager(mrRedisConnectionParam);
        JTreeUtils.processLoading(TreeCellEventExeContext.of(tree, mutableTreeNode), new TreeCellEventHandle<Void>() {

            @Override
            public void process(TreeCellEventExeContext exeContext, CompletableFuture<Void> cf) throws Exception {
                ReadAction.nonBlocking(() -> {
                    valueDisplayPanel.init(project, key, redisPoolManager, mrRedisDb);
                    return null;
                }).finishOnUiThread(ModalityState.defaultModalityState(), p -> {
                    mrRedisEditorJpanelLeft.getTabContentRightShow().updatePanel(valueDisplayPanel);
                    cf.complete(null);
                }).inSmartMode(project).submit(NonUrgentExecutor.getInstance());
            }
        });
    }

    @Override
    public ActionGroup rightClickActionGroup(JTree tree, DefaultMutableTreeNode mutableTreeNode, DefaultActionGroup defaultActionGroup, TreePanelMark treePanelMark) {
        defaultActionGroup.add(new MrRedisReloadKeyValueAction(treePanelMark));
        return defaultActionGroup;
    }

    public Project getProject() {
        return project;
    }

    public MrRedisConnectionParam getMrRedisConnectionParam() {
        return mrRedisConnectionParam;
    }

    public String getRedisKey() {
        return redisKey;
    }

    public void setRedisKey(String redisKey) {
        this.redisKey = redisKey;
    }

    public void reloadValue() {
        if (null != valueDisplayPanel) {
            valueDisplayPanel.doReload();
        }
    }
}
